<?php

require('common.php');

html_header("add a step");

// ii stands for insert instance and is meant to be called within the add_instance hypertext
function ii($call)
{
	global $domain, $line;
	$uri = "insert_instance.php?domain=$domain&amp;line=$line&amp;call=$call";
	echo $uri;
}

// return instead of echo
function return_ii($call)
{
	global $domain, $line;
	$uri = "insert_instance.php?domain=$domain&amp;line=$line&amp;call=$call";
	return $uri;
}

// all the user-defined scripts that we can call 
// TODO: should not include the scripts that forms return to
function my_scripts()
{
	global $domain, $line, $user;
	$result = sql_exec("SELECT * FROM Definitions WHERE User='$user' ");
	while ($row = mysql_fetch_array($result))
	{
		$call = $row['Fxn_Call'];
		$name = $row['English_Name'];
		$desc = $row['Description'];
		$desc = stripslashes($desc);
		$html .= "<dt><a href=\"insert_instance.php?domain=$domain&amp;line=$line&amp;call=$call\">$name</a></dt>\n<dd>$desc</dd>\n";
	}
	return $html;
}

// make links for each table to make things simpler later on
function go_through_table_x()
{
	global $domain, $line;
	// find all the tables 
	$userno = user() . 'no';
	$result = sql_exec("SHOW TABLES LIKE '$userno%' ");
	$html = '';
	
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		// $field holds the name of the real table in the real database (userno...)
		// $table_name is the table name as the user sees it
		$field = array_pop($row);
		$table_name = remove_userno($field);
		$html .= '<dt><a href="insert_instance.php?table=' . $field;
		$html .= '&domain=' . $domain . '&line=' . $line . '&call=database">';
		$html .= "go through $table_name" . '</a></dt>' . "\n";
		$html .= '<dd>go through all or part of ' . $table_name . '</dd>' . "\n";
	}
	
	return $html;
}

// like store_in_table_x, we want to make links for each table we want to delete from 
// so we only have to worry about the WHERE clause in DELETE FROM table WHERE ID=x
function delete_from_table_x()
{
	global $domain, $line;
	// find all the tables 
	$userno = user() . 'no';
	$result = sql_exec("SHOW TABLES LIKE '$userno%' ");
	$html = '';
	
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		// $field holds the name of the real table in the real database (userno...)
		// $table_name is the table name as the user sees it
		$field = array_pop($row);
		$table_name = remove_userno($field);
		$html .= '<dt><a href="insert_instance.php?table=' . $field;
		$html .= '&domain=' . $domain . '&line=' . $line . '&call=delete_from_table">';
		$html .= "delete from $table_name" . '</a></dt>' . "\n";
		$html .= '<dd>delete a row from table ' . $table_name . '</dd>' . "\n";
	}
	
	return $html;
}

// make a link for each table to make things simpler later on
function store_in_table_x()
{
	global $domain, $line;
	// find all the tables 
	$userno = user() . 'no';
	$result = sql_exec("SHOW TABLES LIKE '$userno%' ");
	$html = '';
	
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		// $field holds the name of the real table in the real database (userno...)
		// $table_name is the table name as the user sees it
		$field = array_pop($row);
		$table_name = remove_userno($field);
		$html .= '<dt><a href="insert_instance.php?table=' . $field;
		$html .= '&domain=' . $domain . '&line=' . $line . '&call=store_in_table">';
		$html .= "Store in $table_name" . '</a></dt>' . "\n";
		$html .= '<dd>Add a row to table ' . $table_name . '</dd>' . "\n";
	}
	
	return $html;
}

// returns TRUE if this line is between a 'form' and 'endform', FALSE otherwise
function inside_form($domain, $start_line)
{
	global $user;
	// start at line and go backwards, return TRUE if a 'form' is encountered and FALSE if an 'endform' is encountered
	// except for the line itself, when it is the other way around
	$result = sql_exec("SELECT Fxn_Call, Line_Number FROM Instances WHERE User='$user' AND Domain='$domain' AND Line_Number<='$start_line' ORDER BY Line_Number DESC");
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$call = $row['Fxn_Call'];
		$line = $row['Line_Number'];
		//die(var_dump($row));
		
		if ( ($line == $start_line && $call == 'endform') || ($line < $start_line && $call == 'form') )
		{
			return TRUE;
		}
		
		// we could skip this test since it would return FALSE anyway but maybe it will save some CPU cycles
		if ( ($line == $start_line && $call == 'form') || ($line < $start_line && $call == 'endform') )
		{
			return FALSE;
		}
	}
	return FALSE;
}

// returns TRUE if this line is between 'start' and 'end'
function inside_something($domain, $start_line, $start, $end)
{
	global $user;
	// start at line and go backwards, return TRUE if a 'form' is encountered and FALSE if an 'endform' is encountered
	// except for the line itself, when it is the other way around
	$result = sql_exec("SELECT Fxn_Call, Line_Number FROM Instances WHERE User='$user' AND Domain='$domain' AND Line_Number<='$start_line' ORDER BY Line_Number DESC");
	while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{
		$call = $row['Fxn_Call'];
		$line = $row['Line_Number'];
		//die(var_dump($row));
		
		if ( ($line == $start_line && $call == $end) || ($line < $start_line && $call == $start) )
		{
			return TRUE;
		}
		
		// we could skip this test since it would return FALSE anyway but maybe it will save some CPU cycles
		if ( ($line == $start_line && $call == $start) || ($line < $start_line && $call == $end) )
		{
			return FALSE;
		}
	}
	return FALSE;
}

// fetches the Description field value from the Definitions table
// echoes it NOT returns it
function desc($fxn_call)
{
	$row = sql_row1("SELECT Description FROM Definitions WHERE Fxn_Call='$fxn_call' ");
	if (!empty($row))
	{
		$description = array_pop($row);
		echo $description;
	} else {
		die("empty description");
	}
}

// echoes NOT repeats the <dt> and <dd> for a function with a given name
function name_and_desc($fxn_call)
{
	// fetch the English name
	$row = sql_row1("SELECT English_Name FROM Definitions WHERE Fxn_Call='$fxn_call' ");
	if (!empty($row))
	{
		$name = array_pop($row);
	}
	echo "<dt><a href=\"" . return_ii($fxn_call) . "\">" . $name . "</a></dt>";
	echo "\n<dd>";
	desc($fxn_call);
	echo "</dd>";
}

// calls name_and_desc for a whole array of functions
function renzoku($functions)
{
	foreach ($functions as $f)
	{
		name_and_desc($f);
	}
}

// calls name_and_desc for a whole category
function category($category)
{
	$fxns = sql_exec("SELECT Fxn_Call FROM Definitions WHERE Category='$category' ORDER BY English_Name ");
	
	while ($row = mysql_fetch_array($fxns, MYSQL_ASSOC))
	{
		name_and_desc($row['Fxn_Call']);
	}
}

?>

<body>

<div id="container">

<?php header_div(); ?>

<div id="body">

<h4>most commonly used steps</h4>

<dl>
<?php name_and_desc('echo') ?>
<?php name_and_desc('arithmetic') ?>
<?php name_and_desc('store') ?>
<?php name_and_desc('store_constant') ?>
<?php name_and_desc('concatenate') ?>
<?php name_and_desc('for') ?>
</dl>

<h4>complete list</h4>

<ol>

<li><p>flow</p>
<dl>
<?php 
category('Flow');
?>
</dl>
</li>

<li>
<p>variables and tables</p>
<dl>
<?php category('Memory'); ?>
<?php echo store_in_table_x(); ?>
</dl>
<?php echo go_through_table_x(); ?>
<?php echo delete_from_table_x(); ?>
</li>




<li>
<p>printing stuff and using forms</p>
<dl>
<?php category('IO'); 

if (inside_form($domain, $line) == TRUE)
{
	name_and_desc('form_input');
	name_and_desc('select');

	if (inside_something($domain, $line, 'select', 'endselect') == TRUE)
	{
		name_and_desc('option');
	}
	
	/*
	if (inside_something($domain, $line, 'ol', 'endol') == TRUE || inside_something($domain, $line, 'ul', 'endul') == TRUE)
	{
		name_and_desc('li');
	}
	*/
}
?>
</dl>
</li>

<li>
<p>all the pages</p>
<dl>
<?php echo my_scripts(); ?>
</dl>
</li>

<li>
<p>text</p>
<dl>
<?php category('Text'); ?>
</dl>
</li>


<li>
<p>date and time</p>
<dl>
<?php category('Time'); ?>
</dl>
</li>



<li>
<p>math</p>
<dl>
<?php category('Math'); ?>
</dl>
</li>

</ol>

<?php echo footer(); ?>

</div>



</div>
</body>
</html>





